<?php
//封装算法和函数
class Tool {

    //获取ip
    public static function getIp() {
        return $_SERVER['REMOTE_ADDR'];
    }

    //获取当前日期
    public static function getDate() {
        return date('Y-m-d H:i:s');
    }

    //生成订单
    public function getOrderNum() {
        return date('YmdHis'.mt_rand(0,99999));
    }

    //获得上一页
    public static function getPrevPage() {
        return !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '###';
    }

    public static function setHtmlString($data) {
        $newData = '';
        if(Validate::isArray($data)) {
            if(Validate::isNullArray($data)) return;    //如果是一个空数组，则直接返回，防止把空数组过滤了
            foreach($data as $key=>$value) {
                $newData[$key] = self::setHtmlString($value);
            }
        } else if(Validate::isObject($data)) {
            foreach($data as $key=>$value) {
                $newData->$key = self::setHtmlString($value);
            }
        } else {
            return htmlspecialchars($data);
        }
        return $newData;
    }

    //转义表单数据
    public static function setFormData($string) {
        //检查系统是否已经开启gpc自动过滤，如果开启了就不要手动转义，否则会造成二次转义
        if(!get_magic_quotes_gpc()) {
            //检查传入的数据是否是一个数组，如果是就遍历数组元素，然后再逐个递归调用本方法来进行转义
            if(Validate::isArray($string)) {
                foreach($string as $key=>$value) {
                    //组成一个新的数组
                    $string[$key] = self::setFormData($value);
                }
            } else {
                return addslashes($string);
            }
            return $string;
        }
        return $string;
    }

    //把对象数组转变为普通数组
    public static function setObjectArray($data,$key,$value) {
        $newData = '';
        if(Validate::isArray($data)) {
            foreach($data as $v) {
                $newData[$v->$key] = $v->$value;
            }
        }
        return $newData;
    }

    //使字符串大写
    public static function upToStr($data) {
        return strtoupper($data);
    }

    //使字符串小写
    public static function downToStr($data) {
        return strtolower($data);
    }

    //转义$_GET和$_POST数据
    public static function formatGP() {
        if(isset($_GET)) $_GET = Tool::setFormData($_GET);
        if(isset($_POST)) $_POST = Tool::setFormData($_POST);
    }

    //弹窗传值并关闭(用于上传文件)
    public static function alertSetValueClose($value,$info) {
        echo <<<END
        <script type='text/javascript'>
            alert('$info');
            var a = opener.document.add || opener.document.update;
            a.thumbnail.value='$value';
            a.pic.style.display='block';
            a.pic.src = '$value';
            window.close();
        </script>
END;
    }

    //获取当前url
    public static function getUrl() {
        $url = $_SERVER['REQUEST_URI'];
        $path = parse_url($url);
        if(isset($path['query'])) {
            parse_str($path['query'],$query);
        }
        $url = $path['path'].'?'.http_build_query($query);
        return $url;
    }

    //获取域名
    public static function getDomain() {
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        if(strpos($url,'index.php') !== false) {
            $url = substr($url,0,strpos($url,'index.php'));
        } else {
            $url = substr($url,0,strpos($url,'?'));
        }
        return $url;
    }



}


?>